//
//  AddCaseController.m
//  FastDoctor
//
//  Created by 程建良 on 16/7/8.
//  Copyright © 2016年 sc. All rights reserved.
//

#import "AddCaseController.h"
#import "AddCaseCell.h"
#import "MeicalModel.h"
#import "CaseAddDesView.h"
#import "DetailDiseaseController.h"
#import "DiseaseTypeController.h"
#import "UIImage+Scale.h"
@interface AddCaseController ()<UITableViewDataSource,UITableViewDelegate,UITextViewDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property (nonatomic,weak)UITableView *tableView;
@property (nonatomic,weak)CaseAddDesView *desView;
@property (nonatomic,copy) NSString *diseaseText;
@property (nonatomic,strong)UIImage *buttonImage;
@property (nonatomic,copy) NSString *typeText;
@property (nonatomic,weak)UIBarButtonItem *rightButonItem;
@end

@implementation AddCaseController
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    if (![touches.anyObject.view
         isKindOfClass:[UITextView class]]) {
        [self.desView.desTf resignFirstResponder];
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationItem.title = @"增加病例";
    [self setupUI];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem  alloc]initWithTitle:@"提交" style:UIBarButtonItemStylePlain target:self action:@selector(updataInfo)];
    self.rightButonItem = self.navigationItem.rightBarButtonItem;
    self.rightButonItem.tintColor = [UIColor blackColor];
}
-(void)updataInfo{
    MeicalModel *model = [[MeicalModel alloc]init];
    NSData*data1 = UIImageJPEGRepresentation(self.buttonImage, 1.0);
    NSData*data2 = UIImageJPEGRepresentation( [UIImage imageNamed:@"compose_pic_add_highlighted"], 1.0);
    if([data1 isEqualToData:data2]){
        model.medicalImage = nil;
    }else{
        model.medicalImage = self.buttonImage;
    }
    model.medicalDescrip = self.diseaseText;
    model.medicalName = self.typeText;
    model.medicalDetail =self.desView.desTf.text;
    if ([self.delegate respondsToSelector:@selector(addCaseController:WithModel:)]) {
        [self.delegate addCaseController:self WithModel:model];
    }
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)didClickImageButton{
     UIImagePickerController *pickerVc = [[UIImagePickerController alloc]init];
    pickerVc.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    pickerVc.delegate = self;
    [self presentViewController:pickerVc animated:YES completion:nil];
}
#pragma mark - UI
-(void)setupUI{
    [self setupTableView];
    [self setupDesView];
}
-(void)setupTableView{
    UITableView *tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.tableView = tableView;
    tableView.delegate = self;
    tableView.dataSource = self;
    [self.view addSubview:tableView];
    [self.tableView registerClass:[AddCaseCell class] forCellReuseIdentifier:@"cell"];
}
-(void)setupDesView{
    CaseAddDesView *view = [[CaseAddDesView alloc]init];
    self.desView = view;
    self.desView.desTf.delegate = self;
    [self.view addSubview:view];
    [self.desView.imageButton addTarget:self action:@selector(didClickImageButton) forControlEvents:   UIControlEventTouchUpInside];
    self.buttonImage = [UIImage imageNamed:@"compose_pic_add_highlighted"];
}
#pragma mark - tableViewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 2;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
     AddCaseCell*cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    if (indexPath.row == 0) {
        cell.textLabel.text = @"疾病类型";

        NSString *diseaseType = self.typeText;
        if (diseaseType) {
            cell.selectLabel.text  = diseaseType;
            
        }else{
            cell.selectLabel.text = @"请选择";
        }
    }else if(indexPath.row == 1){
        cell.textLabel.text = @"疾病细分";
        
        NSString *diseaseName = self.diseaseText;
        if (diseaseName) {
            cell.selectLabel.text  = diseaseName;
            
        }else{
            cell.selectLabel.text = @"请选择";
        }
    }
    return cell;
}
#pragma mark - masonry
-(void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(64);
        make.left.right.offset(0);
        make.height.mas_equalTo(100);
    }];
    if (kDevice_Is_iPhone5) {
        [self.desView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.tableView.mas_bottom).offset(10);
            make.left.right.offset(0);
            make.bottom.offset(-100);
        }];
    }else{
        [self.desView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.tableView.mas_bottom).offset(10);
            make.left.right.offset(0);
            make.bottom.offset(-200);
        }];
    }
    if (self.typeText && self.diseaseText) {
        self.rightButonItem.enabled = YES;
    }else{
        self.rightButonItem.enabled = NO;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == 0) {
        
        DiseaseTypeController *typeController = [[DiseaseTypeController alloc]init];
        
        typeController.title = @"疾病类型";
        typeController.typeBlock = ^(NSString *string){
            self.typeText = string;
        };
        [self.navigationController pushViewController:typeController animated:YES];
        
    }else if (indexPath.row == 1) {
        
        DetailDiseaseController *detailVC = [[DetailDiseaseController alloc]init];
        
        detailVC.block = ^(NSString *string) {
            
            self.diseaseText = string;
        };
        [self.navigationController pushViewController:detailVC animated:YES];
    }
}

#pragma mark - textViewDelegate
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    if ([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
    }
    return YES;
}


- (void)setDiseaseText:(NSString *)diseaseText {
    _diseaseText = diseaseText;
    [self.tableView reloadData];
}
#pragma mark -imagePickerControllerDelegate
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    UIImage *image = info[UIImagePickerControllerOriginalImage];
    image = [image scaleToSize:CGSizeMake(80, 80)];
    [self.desView.imageButton setBackgroundImage:image forState:UIControlStateNormal];
    self.buttonImage = image;
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)setTypeText:(NSString *)typeText {
    
    _typeText = typeText;
    [self.tableView reloadData];
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
